/*
 * File     : AbstractAutoFreedStructureFactory.java
 * Created  : 24 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.structure;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.googlecode.dni.type.LifetimeManagedObject;
import com.googlecode.dni.type.Pointer;
import com.googlecode.dni.type.structure.Structure;


/**
 * <p>
 *  Provides the base functionality for {@link Structure} types that are not
 *  {@link LifetimeManagedObject}.
 * </p>
 * <p>
 *  Note how much extra overhead is required for maintaining the consistency of
 *  just one Java object for a given allocated {@link Structure}.
 * </p>
 *
 * @param <T>
 *            the type of structure
 *
 * @author Matthew Wilson
 */
public abstract class AbstractAutoFreedStructureFactory< T extends Structure >
    extends AbstractStructureFactory< T >
{

    private final Map< Pointer, WeakReference< T > > createdInstances =
        new ConcurrentHashMap< Pointer, WeakReference< T > >();


    /**
     * @param structureType
     *            the structure interface type
     */
    protected AbstractAutoFreedStructureFactory( final Class< T > structureType )
    {
        super( structureType );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final T allocate()
    {
        @SuppressWarnings( "unchecked" )
        T structure = (T) allocateUntyped();

        this.createdInstances.put( structure.pointer(), new WeakReference< T >( structure ) );

        return structure;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final T fromPointer( final Pointer pointer )
    {
        long address = pointer.getAddress();
        if ( address == 0L )
        {
            return null;
        }

        WeakReference< T > reference = this.createdInstances.get( pointer );
        T structure = reference != null ? reference.get() : null;

        if ( structure != null )
        {
            return structure;
        }

        // NOT created from memory we allocated, so no need to remember it
        @SuppressWarnings( "unchecked" )
        T yayThisIsFun = (T) fromPointerUntyped( address );
        return yayThisIsFun;
    }

    /**
     * Removes an instance created from allocated memory.
     *
     * @param pointer
     *            the pointer
     */
    public final void remove( final long pointer )
    {
        this.createdInstances.remove( new Pointer( pointer ) );
    }

}
